package service.major;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import util.manager.PageManager;
import util.paramvo.SearchParamVO;
import util.returnvo.MajorSearchListReturnVO;
import vo.MajorVO;
import config.PageConfig;
import dao.major.MajorDAO;

public class MajorServiceImpl implements MajorService{

	private MajorDAO majorDAO;

	public void setMajorDAO(MajorDAO majorDAO) {
		this.majorDAO = majorDAO;
	}

	@Override
	public MajorSearchListReturnVO getSearchedMajorByWord(String search, String page) throws Exception {
		
		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}
		
		int pageNo=1;
		
		if(page!=null && !page.equals("")) pageNo=Integer.parseInt(page);
		
		List<MajorVO> majorlist=majorDAO.getSearchedMajorByWord(new SearchParamVO(searchlist, PageConfig.POPUP_ROW_PER_PAGE, pageNo));
		
		return new MajorSearchListReturnVO(majorlist, new PageManager(PageConfig.POPUP_ROW_PER_PAGE, PageConfig.POPUP_PAGE_PER_GROUP, majorDAO.countSearchedMajorByWord(searchlist), pageNo));
	}

	@Override
	public List<MajorVO> getMajorEditList(String search) throws Exception {

		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}else return new ArrayList<MajorVO>();
		
		return majorDAO.getSearchedMajorByWord(new SearchParamVO(searchlist, PageConfig.EDIT_ROW_PER_PAGE, 1));
	}

	@Override
	public List<String> autoCompleteMajorList(String search) throws Exception {
		
		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}else return new ArrayList<String>();
		
		List<MajorVO> majorlist=majorDAO.getSearchedMajorByWord(new SearchParamVO(searchlist, PageConfig.AUTOCOMPL_ROW_PER_PAGE, 1));
		
		ArrayList<String> returnlist=new ArrayList<String>();
		
		for(MajorVO vo : majorlist){
			returnlist.add(vo.getMajor_name());
		}
		
		return returnlist;
	}

	@Override
	public void registerMajor(String Major_name) throws Exception {
		majorDAO.registerMajor(Major_name);
	}

	@Override
	public void updateMajor(MajorVO param) throws Exception {
		majorDAO.updateMajor(param);
	}

	@Override
	public void deleteMajor(int major_no) throws Exception {
		majorDAO.deleteMajor(major_no);
	}
}
